import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import VueQuillEditor from 'vue-quill-editor'

Vue.config.productionTip = false
import "@/assets/index.css"; // 全局样式

import "@/assets/iconFont/iconfont.css";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

// 导入自定义权限指令
import permissionDirective from './directive/permission'

// 全局注册指令
Vue.directive('permission', permissionDirective)

// 引入 Quill 样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(VueQuillEditor)

router.afterEach((to) => {
  document.title = to.meta.title || '后台管理';
});

// 恢复 Vuex 状态
const savedPermissions = (() => {
  try {
    return JSON.parse(localStorage.getItem("permissions")) || [];
  } catch (error) {
    console.error("解析 permissions 时出错:", error);
    return [];
  }
})();
const savedUsername = localStorage.getItem("username") || "";
store.dispatch("permissions/setPermissions", savedPermissions); // 恢复权限信息
store.dispatch("permissions/setUsername", savedUsername); // 恢复用户名

// 动态添加路由
import { addAsyncRoutes } from "@/router";
addAsyncRoutes(savedPermissions); // 根据权限动态添加路由

// 在创建Vue实例前，初始化权限状态
store.dispatch('permissions/initState').then(() => {
  console.log('权限状态初始化完成，isAdmin:', store.getters['permissions/isAdmin']);
  
  // 创建Vue实例
  new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount('#app')
});

// 如果上述代码不适合直接修改main.js，则可以在App.vue的created钩子中初始化
